package com.countertack.testcases;


//automation for bug # 9552
import java.net.InetAddress;
import java.sql.ResultSet;

import com.countertack.impala.ImpalaConnector;
import com.countertack.ssh.SSHManager;
import com.countertack.utils.Configuration;
import com.countertack.utils.HttpClientUtils;
import com.countertack.utils.Utils;
import com.countertack.vmware.VMWareConnector;
import com.vmware.vim25.mo.VirtualMachine;

public class StartStopTest {

	public static void main(String[] args) throws Exception {
		StringBuffer testCaseResult = new StringBuffer();

	try {

			if (args.length <= 0) {

				System.out.println("configuration details missed !");

				return;
			}

			String configFilePath = args[0];
	//	String configFilePath="C:\\Users\\user\\Desktop\\9552\\config.properties";
			Configuration.loadProperties(configFilePath);
			String vmUser = Configuration.getProperty("vmuser");
			String vmPassword = Configuration.getProperty("vmpassword");
			VMWareConnector esxiConnector = new VMWareConnector(
					Configuration.getProperty("esxi"));
			VirtualMachine endPointMachine = esxiConnector
					.getVirtualMachineByname(Configuration
							.getProperty("vmname"));
			int count=Integer.parseInt(Configuration.getProperty("rebootcount"));

			// win7x64-sym-2
			System.out.println("power state >>>"
					+ endPointMachine.getRuntime().powerState);

			String endpointMachineStatus = null;

			// System.out.println("VM Ip Address>>>" + vmIp);
			String smokeSnapShotName = Configuration
					.getProperty("rootsnapshotname");
			esxiConnector.revertToSnapShort(endPointMachine.getSnapshot()
					.getRootSnapshotList(), smokeSnapShotName);

			System.out.println("Reverting to root snap shot>>>>>"
					+ smokeSnapShotName);

			esxiConnector.checkAndRunVirtualMachine(endPointMachine);
			String vmIp = Configuration.getProperty("vmip");
			if (vmIp == null || vmIp.trim() == "") {

				vmIp = endPointMachine.getGuest().ipAddress;
			}
			System.out.println("ip>>>" + endPointMachine.getGuest().ipAddress);
			InetAddress address = InetAddress.getByName(Configuration
					.getProperty("sentinelehost"));
			String sentenelIp = address.getHostAddress();
			testCaseResult.append("VM name>>>"
					+ Configuration.getProperty("vmname") + "\n");
			testCaseResult.append("ESXI>>>>" + testCaseResult.append("esxi")
					+ "\n");

			testCaseResult.append("Sentinel>>>" + sentenelIp + "\n");

			

				try {

					SSHManager endpointSsh = new SSHManager(vmUser, vmPassword,
							vmIp);
					endpointSsh.connect();

					endpointSsh.copyFile(
							Configuration.getProperty("driverpath"),
							"CounterTack Agent Installer Hiding.exe");

					boolean isXp = Boolean.valueOf(Configuration
							.getProperty("isxpmachine"));
					testCaseResult
							.append("<==================================>"
									+ "\n");
					if (isXp) {
						endpointSsh
								.runCommand("cmd /c \"C:\\Documents and Settings\\"
										+ vmUser

										+ "\\CounterTack Agent Installer Hiding.exe\" /S /HOST:"
										+ sentenelIp
										+ " /PORT:443");

					} else {

						endpointSsh
								.runCommand("cmd /c \"C:\\Users\\"
										+ vmUser

										+ "\\CounterTack Agent Installer Hiding.exe\" /S /HOST:"
										+ sentenelIp + " /PORT:443");

					}

					System.out.println("Installing agent on endpoint");
					testCaseResult
							.append("Installing agent on endpoint" + "\n");

					Thread.sleep(120000);

					String eId = null;
					ImpalaConnector iConnector = new ImpalaConnector();
					iConnector.createImpalaConnection(sentenelIp);
					ResultSet endpointIdRes = iConnector
							.executeQueryAndReturnResults("select id from endpoints where ip='"
									+ vmIp

									+ "'"
									+ " and status='ON' and ip is not null order by time_started desc limit 1");

					while (endpointIdRes.next()) {
						try {
							eId = endpointIdRes.getString(1);
						} catch (Exception e) {

						}

					}
					System.out.println("endpoint Id>>>>>>" + eId);
					testCaseResult.append("endpoint Id>>>>>>" + eId + "\n");
					
					
					endpointSsh.doReconnect=false;
					for(int i=0;i<count;i++){
						
						System.out.println("Rebooting machine..");
						esxiConnector.reStartVirtualMachine(endPointMachine);
						Thread.sleep(60000);
						while (true) {

							if ("guestToolsRunning"
									.equalsIgnoreCase(endPointMachine.getGuest().toolsRunningStatus)) {
								
								break;

							}
							Thread.sleep(30000);
						}
						
						System.out.println("Reboot completed......");
						
						Thread.sleep(300000);
					}
				

				} catch (Exception e) {

					System.out.println("exception");
					e.printStackTrace();
				}

			

		} finally {
			Utils.addResultsToLog(testCaseResult, "verifyingbug#9552_result",
					Configuration.getProperty("vmname"));

		}

	}
}

